const api = require('../../utils/api.js');
const tools = require('../../utils/getuserinfo.js');

let app = getApp();
const { module: moduleName } = app.siteInfo;
const util = app.util;

Page({
  data: {
    score: [1, 2, 3, 4, 5],
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },

  onLoad(opts) {
    let options = opts || {};
    let user_id = options.user_id;
    let share_scene = 4;
    app.tabbarindex = options.tabbarindex
      ? options.tabbarindex - 0
      : app.tabbarindex;
    app.pageOnLoad(this);
    let that = this;
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    let userinfo = wx.getStorageSync('user_info');
    user_id && (app.ws_micro_shop_globalData.parent_id = user_id);
    if (userinfo) {
      Number(userinfo.info) === 0 && tools.onGetInfo(this, userinfo.auth_mode);
      app.loginBindParent({
        parent_id: user_id,
        shop_id: options.shopid,
        share_scene,
      });
      util.prepareBindInfo(options, moduleName, that);
    } else {
      app.ws_micro_shoplogin().then(res => {
        userinfo = wx.getStorageSync('user_info');
        Number(userinfo.info) === 0 &&
          tools.onGetInfo(that, userinfo.auth_mode);
        util.prepareBindInfo(options, moduleName, that);
        app.getStoreData().then(res => {
          app.loginBindParent({
            parent_id: user_id,
            shop_id: options.shopid,
            share_scene,
          });
        });
      });
    }
    that.setData({
      shop_id: options.shop_id,
      opts: options,
    });
    wx.showLoading({
      title: '加载中',
    });
    app.ws_micro_shoprequest({
      url: api.default.shop_detail,
      method: 'GET',
      data: {
        shop_id: options.shop_id,
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData(res.data);
          let detail = res.data.shop.content
            ? res.data.shop.content
            : '<span>暂无信息</span>';
          that.setData({
            detail,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
            success: function(e) {
              if (e.confirm) {
                wx.redirectTo({
                  url: '/ws_micro_shop/pages/shop/shop',
                });
              }
            },
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  mobile() {
    wx.makePhoneCall({
      phoneNumber: this.data.shop.mobile,
    });
  },

  goto() {
    let that = this;
    wx.getSetting({
      success(res) {
        if (typeof res.authSetting['scope.userLocation'] === 'undefined') {
          that.location();
        } else if (res.authSetting['scope.userLocation'] === false) {
          that.setData({
            open_setting: true,
          });
        } else {
          that.location();
        }
      },
    });
  },

  location() {
    let shop = this.data.shop;
    wx.openLocation({
      latitude: parseFloat(shop.latitude),
      longitude: parseFloat(shop.longitude),
      name: shop.name,
      address: shop.address,
    });
  },

  onShareAppMessage() {
    let user_info = wx.getStorageSync('user_info');
    let shop_id = this.data.shop_id;
    let path =
      '/ws_micro_shop/pages/shop-detail/shop-detail?user_id=' +
      user_info.id +
      '&tabbarindex=' +
      app.tabbarindex +
      '&shopid=' +
      app.ws_micro_shop_globalData.shopid;
    path = util.spliceSharePath(path, '4', moduleName);
    return {
      path,
    };
  },
  onShareTimeline() {
    let user_info = wx.getStorageSync('user_info');
    return {
      title: '',
      imageUrl: '',
      query: 'user_id=' +
      user_info.id +
      '&tabbarindex=' +
      app.tabbarindex +
      '&shopid=' +
      app.ws_micro_shop_globalData.shopid
    }
  },

  onHide() {
    util.sendBindInfo(this.data);
  },

  onUnload() {
    util.sendBindInfo(this.data);
  },
});
